//
//  EllipseMode.m
//  dataGathering
//
//  Created by apple on 14/12/24.
//  Copyright (c) 2014年 shuili. All rights reserved.
//

#import "EllipseMode.h"

@interface EllipseMode ()
@property(nonatomic, retain)AGSPoint* beginPoint;

@end

@implementation EllipseMode

-(instancetype)initWithMapview :(AGSMapView*)mapview
{
    return [super initWithMapview:mapview];
}

-(void)mapView:(AGSMapView *)mapView didTapAndHoldAtPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint features:(NSDictionary *)features
{
    if([self.myPolygon numPoints] > 0){
        while([self.myPolygon numPoints] > 0){
            [self.myPolygon removePointOnRing:0 atIndex:0];
        }
        [self.myGraphic setGeometry:self.myPolygon];
        _beginPoint = mappoint;
    } else {
        self.myGraphic = [[AGSGraphic alloc] initWithGeometry:mappoint symbol:self.innerSymbol attributes:nil];
        [self.myGraphicsLayer addGraphic:self.myGraphic];
        _beginPoint = mappoint;
    }
    
}

-(void)mapView:(AGSMapView *)mapView didMoveTapAndHoldAtPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint features:(NSDictionary *)features
{
    NSLog(@"moving");

    self.myPolygon = [[AGSGeometryEngine defaultGeometryEngine]  bufferGeometry:_beginPoint byDistance:[[AGSGeometryEngine defaultGeometryEngine] distanceFromGeometry:_beginPoint toGeometry:mappoint]];
    [self.myGraphic setGeometry:self.myPolygon];
    
}

-(void)mapView:(AGSMapView *)mapView didEndTapAndHoldAtPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint features:(NSDictionary *)features
{
    NSLog(@"end");
    
    double r = [[[AGSGeometryEngine defaultGeometryEngine] geodesicDistanceBetweenPoint1:_beginPoint point2:mappoint inUnit:AGSSRUnitMeter] distance];
    
    NSLog(@"length is %f", r);
    
    AGSGeodesicEllipseParameters *para = [AGSGeodesicEllipseParameters parametersWithCenter:_beginPoint maxSegmentLength:r/10.0 semiAxisLength:r];
    //para.semiAxis1Length = r;
    //para.semiAxis2Length = para.semiAxis1Length;
    NSLog(@"axis1 length is %f ,axis2 length is %f", para.semiAxis1Length, para.semiAxis2Length);
    
    
    //self.myPolygon = (AGSMutablePolygon*)[[AGSGeometryEngine defaultGeometryEngine] geodesicEllipseWithParameters:para];
    
    self.myPolygon = [[AGSGeometryEngine defaultGeometryEngine]  bufferGeometry:_beginPoint byDistance:[[AGSGeometryEngine defaultGeometryEngine] distanceFromGeometry:_beginPoint toGeometry:mappoint]];

    [self.myGraphic setGeometry:self.myPolygon];
    
    NSLog(@"ellipse vertex count is %ld", (long)[self.myPolygon numPoints]);
    NSLog(@"ellipse area count is %f", [[AGSGeometryEngine defaultGeometryEngine] areaOfGeometry:self.myPolygon]);
    
    //[self stop];
    
    //if (self.delegate && [self.delegate respondsToSelector:@selector(BaseMode:didFinishDrawing::)]) {
    //    [self.delegate BaseMode:self didFinishDrawing:self.myPolygon :@"POLYGON"];
    //}
}

-(void)mapView:(AGSMapView *)mapView didClickAtPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint features:(NSDictionary *)features
{
    if([[AGSGeometryEngine defaultGeometryEngine] geometry:mappoint intersectsGeometry:self.myPolygon])
    {
        [self stop];
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(BaseMode:didFinishDrawing::)]) {
            [self.delegate BaseMode:self didFinishDrawing:self.myPolygon :@"POLYGON"];
        }
    }
    
}


@end
